package shopping.worker;

import com.jd.platform.async.callback.ICallback;
import com.jd.platform.async.callback.IWorker;
import com.jd.platform.async.worker.WorkResult;
import com.jd.platform.async.wrapper.WorkerWrapper;
import shopping.mode.Order;
import shopping.mode.TempOrder;
import sun.awt.windows.WSystemTrayPeer;

import java.util.HashMap;
import java.util.Map;

/**
 * 生成订单
 *
 */
public class CreatedOrderWorker implements IWorker<TempOrder,Map<String,Object>> , ICallback<TempOrder,Map<String,Object>> {


    /**
     * 第一个被调用
     */
    @Override
    public void begin() {
        //ICallback.super.begin();
        /**
         * @TODO 初始化参数
         * 比如初始化需要得service服务对象
         */
        System.err.println("创建订单worker开始---------------");

    }

    /**
     * 核心处理点
     * @param object      object  传入参数
     * @param allWrappers 任务包装  其他线程包裹对象
     * @return
     */
    @Override
    public Map<String,Object> action(TempOrder object, Map<String, WorkerWrapper> allWrappers) {
        // WorkerWrapper workerWrapper = allWrappers.get("one");  可以通过该对象获取其他得线程对象
        /**
         * 1、接收参数
         * 2、对象参数
         * 3、创建订单
         */
        Map<String,Object> orderMap = ( Map<String,Object>)allWrappers.get("manJianWorker").getWorkResult().getResult();
        Map<String,Object> addreeMap = ( Map<String,Object>)allWrappers.get("addreeWorker").getWorkResult().getResult();

        /**
         * 校验满减
         */
        if(!(boolean)orderMap.get("success")) {
            return orderMap;
        }
        /**
         * 校验地址
         */
        if(!(boolean)addreeMap.get("success")) {
            return addreeMap;
        }
        /**
         * 创建订单
         */
        Map<String,Object> result = new HashMap<>();
        Order order = (Order) orderMap.get("order");
        order.setAddress(object.getAddress());
        result.put("success",true);
        result.put("order",order);
        result.put("msg","下单成功");
        return result;
    }

    @Override
    public Map<String,Object> defaultValue() {
        Map<String,Object> defaultResult = new HashMap<>();
        defaultResult.put("success",false);
        defaultResult.put("msg","创建订单异常，请联系管理员！");
        return defaultResult;
    }

    @Override
    public void result(boolean success, TempOrder param, WorkResult<Map<String,Object>> workResult) {
        Map<String,Object> result = (Map<String,Object>)workResult.getResult();
        boolean suFlag = (boolean)result.get("success");
        String msg = (String)result.get("msg");
        System.err.println("创建订单worker结束----------：" + suFlag + ",msg:" + msg);
    }

}
